<template>
  <button class="f-button" :class="{[`icon-${iconPosition}`]:true}" @click="$emit('click')">
    <f-icon v-if="icon&&!loading" :name="icon" class="icon"></f-icon>
    <f-icon v-if="loading" name="loading" class="loading icon"></f-icon>
    <div class="f-button-content">
      <slot></slot>
    </div>
  </button>
</template>

<script>
  import FIcon from './icon';
  export default {
    name: 'f-button'
    , props: {
      icon:{type:String}
      ,loading:{
        type:Boolean
        ,default: false
      }
      ,iconPosition:{
        type:String
        ,default:'left'
        ,validator(value) {
          return (value === 'left' || value === 'right');
        }
      }
    }
    ,components:{
      FIcon: FIcon
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss">
  @import "var";
  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }
  .f-button { font-size: $font-size; height: $button-height; padding: 0 1em;
    border-radius: $border-radius; border: 1px solid $border-color;
    background: $button-bg;
    display: inline-flex; justify-content: center; align-items: center;
    vertical-align: middle;
    &:hover { border-color: $border-color-hover; }
    &:active { background-color: $button-active-bg; }
    &:focus { outline: none; }
    > .f-button-content { order: 2; }
    > .icon { order: 1; margin-right: .1em; }

    &.icon-right {
      > .f-button-content { order: 1; }
      > .icon { order: 2; margin-right: 0; margin-left: .1em;}
    }
    .loading {
      animation: spin 2s infinite linear;
    }
  }
</style>
